﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;

using com.vs.DataAccess;
using com.vs.DataAccess.DO;
using com.vs.General;
using com.vs.General.BL;

namespace com.vs.WebService
{
    /// <summary>
    /// Summary description for SecurityService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class SecurityService : System.Web.Services.WebService
    {

        [WebMethod]
        public DataTable GetAllGroupList(string stSessID)
        {
            Session sess = null;
            SecurityManager mgr = null;
            DataTable dt = null;
            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return null;
                mgr = new SecurityManager();
                dt = mgr.GetGroup(sess);
                dt.DataSet.DataSetName = "GroupList";
                dt.TableName = "Group";
                return dt;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }

        }

        [WebMethod]
        public DataTable GetRoleListByGroup(string stSessID, int iIntGroupNum, string stParentFuncValue)
        {
            Session sess = (Session)Application.Get(stSessID);
            SecurityManager mgr = new SecurityManager();
            DataTable dt = mgr.GetRoleListByGroup(sess, iIntGroupNum, stParentFuncValue);
            dt.DataSet.DataSetName = "RoleList";
            dt.TableName = "Role";
            return dt;
        }

        [WebMethod]
        public DataTable GetAllFunctionList(string stSessID)
        {
            Session sess = (Session)Application.Get(stSessID);
            SecurityManager mgr = new SecurityManager();
            DataTable dt = mgr.GetAllFunctionList(sess);
            dt.DataSet.DataSetName = "FunctionValueList";
            dt.TableName = "FunctionValue";
            return dt;
        }

        [WebMethod]
        public DataTable GetFunctionList(string stSessID, string parentFunctionValue)
        {
            Session sess = (Session)Application.Get(stSessID);
            SecurityManager mgr = new SecurityManager();
            DataTable dt = mgr.GetFunctionList(sess, parentFunctionValue);
            dt.DataSet.DataSetName = "FunctionValueList";
            dt.TableName = "FunctionValue";
            return dt;
        }

        [WebMethod]
        public DataTable GetFunctionListOfUser(string stSessID, string userId, string parentFunctionValue)
        {
            Session sess = (Session)Application.Get(stSessID);
            SecurityManager mgr = new SecurityManager();
            DataTable dt = mgr.GetFunctionListOfUser(sess, userId, parentFunctionValue);
            dt.DataSet.DataSetName = "FunctionValueList";
            dt.TableName = "FunctionValue";
            return dt;
        }

        [WebMethod]
        public DataTable GetGroupListOfUser(string stSessID, string userId)
        {
            Session sess = (Session)Application.Get(stSessID);
            SecurityManager mgr = new SecurityManager();
            DataTable dt = mgr.GetGroupListOfUser(sess, userId);
            dt.DataSet.DataSetName = "GroupList";
            dt.TableName = "Group";
            return dt;
        }

        [WebMethod]
        /**
         * 保存权限组设置
         * @groupNum:当前需要变更的权限组ID
         * @addFunctionNums:增加的权限，对应的资源ID值，
         * @addDataValues:增加的权限，对应的DataValue值，每条记录如果有多个值，用","分隔开
         * @deleteNums:需要删除的权限ID
         */
        public string SaveRoleGroup(string stSessID, int groupNum, int[] addFunctionNums, string[] addDataValues, int[] deleteNums)
        {
            Session sess = (Session)Application.Get(stSessID);
            SecurityManager mgr = new SecurityManager();
            string str = mgr.SaveRoleGroup(sess, groupNum, addFunctionNums, addDataValues, deleteNums);
            return str;
        }

        [WebMethod]
        /**
         * 保存角色组，如果传入groupNum值，则更新，否则insert
         */
        public string SaveGroup(string stSessID, int groupNum, String groupName)
        {
            Session sess = (Session)Application.Get(stSessID);
            SecurityManager mgr = new SecurityManager();
            string str = mgr.SaveGroup(sess, groupNum, groupName);
            return str;
        }

        [WebMethod]
        /**
         * 删除角色组
         */
        public string DeleteGroup(string stSessID, int groupNum)
        {
            Session sess = (Session)Application.Get(stSessID);
            SecurityManager mgr = new SecurityManager();
            string str = mgr.DeleteGroup(sess, groupNum);
            return str;
        }

        [WebMethod]
        public string SaveUserGroup(string stSessID, String userId, int[] addGroupNums, int[] deleteNums)
        {
            Session sess = (Session)Application.Get(stSessID);
            SecurityManager mgr = new SecurityManager();
            string str = mgr.SaveUserGroup(sess, userId, addGroupNums, deleteNums);
            return str;
        }
    }
}
